package com.prs.crm.action.fs;

import java.io.File;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.fs.BankAccount;
import com.prs.crm.domain.fs.Outlay;
import com.prs.crm.domain.fs.OutlayDetail;
import com.prs.crm.domain.fs.OutlayReturn;
import com.prs.crm.service.fs.BankAccountService;
import com.prs.crm.service.fs.OutlayReturnService;
import com.prs.crm.service.fs.OutlayService;

public class OutlayReturnAction extends BaseAction {
	private OutlayReturn outlayReturn;
	private OutlayReturnService outlayReturnService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private Outlay outlay;
	private OutlayService outlayService;
	private BankAccountService bankAccountService;
	private String bank;
	private Collection<BankAccount> bankAccounts;

	private File upload;
	private String uploadFileName;

	@Action("listWaitOutlayReturn")
	public String listWaitOutlayReturn() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Outlay.class);
		criteria.add(Restrictions.eq("isReturn", "需要"));
		criteria.add(Restrictions
				.eq("returnFlag", Constrains.FS_FLAG_NOTRETURN));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOutlayService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editOutlayReturn")
	public String editOutlayReturn() {
		bankAccounts = this.getBankAccountService().getAllBankAccount();
		return SUCCESS;
	}

	@Action("listOutlayReturn")
	public String listOutlayReturn() {
		return SUCCESS;
	}

	@Action("listOutlayReturnByBank")
	public String listOutlayReturnByBank() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				OutlayReturn.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		if (bank != null) {
			if (!"all".equals(bank)) {
				bank = bank.split(",")[0];
				if (ToolKits.isInteger(bank)) {
					criteria.createAlias("bankAccount", "ba");
					criteria.add(Restrictions.eq("ba.id", Integer
							.parseInt(bank)));
				}
			}
		}
		this.setPagination(this.getOutlayReturnService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewOutlayReturn")
	public String viewOutlayReturn() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitOutlayReturn", results = {
			@Result(name = "success", type = "redirect", location = "listWaitOutlayReturn"),
			@Result(name = "error", type = "chain", location = "editOutlayReturn") })
	public String submitOutlayReturn() {
		if (outlayReturn.getTitle() == null
				|| "".equals(outlayReturn.getTitle().trim())) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (outlayReturn.getBankAccount() == null) {
			addActionError("账号不能为空");
			return ERROR;
		}
		if (outlayReturn.getReturnTime() == null) {
			addActionError("返还时间不能为空");
			return ERROR;
		}
		if (outlayReturn.getMoney() == null) {
			addActionError("返还金额不能为空");
			return ERROR;
		}
		if (outlayReturn.getReturnPerson() == null) {
			addActionError("返还人不能为空");
			return ERROR;
		}
		outlayReturn.setOutlay(outlay);
		outlayReturn.setState(Constrains.TASK_AUDITING);
		Double totalMoney = 0.0;
		for (OutlayDetail detail : outlayReturn.getOutlay().getOutlayDetails()) {
			totalMoney += detail.getMoney();
		}
		if (outlayReturn.getMoney().doubleValue() != totalMoney.doubleValue()) {
			this.addActionError("返还金额不等于需返还金额");
			return ERROR;
		}
		this.getOutlayReturnService().submitOutlayReturn(outlayReturn, upload,
				uploadFileName);
		this.setPrompt("提交费用返还单成功");
		return SUCCESS;
	}

	public OutlayReturn getOutlayReturn() {
		return outlayReturn;
	}

	public void setOutlayReturn(OutlayReturn outlayReturn) {
		this.outlayReturn = outlayReturn;
	}

	public OutlayReturnService getOutlayReturnService() {
		return outlayReturnService;
	}

	public void setOutlayReturnService(OutlayReturnService outlayReturnService) {
		this.outlayReturnService = outlayReturnService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setOutlayService(OutlayService outlayService) {
		this.outlayService = outlayService;
	}

	public OutlayService getOutlayService() {
		return outlayService;
	}

	public void setOutlay(Outlay outlay) {
		this.outlay = outlay;
	}

	public Outlay getOutlay() {
		return outlay;
	}

	public void setBankAccountService(BankAccountService bankAccountService) {
		this.bankAccountService = bankAccountService;
	}

	public BankAccountService getBankAccountService() {
		return bankAccountService;
	}

	public void setBankAccounts(Collection<BankAccount> bankAccounts) {
		this.bankAccounts = bankAccounts;
	}

	public Collection<BankAccount> getBankAccounts() {
		return bankAccounts;
	}

	public String getBank() {
		return bank;
	}

	public void setBank(String bank) {
		this.bank = bank;
	}

}
